package com.eastfair.projectcore.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eastfair.projectcore.dao.HotImageMapper;
import com.eastfair.projectcore.entity.HotImage;
import com.eastfair.projectcore.service.HotImagePointService;
import com.eastfair.projectcore.service.HotImageService;
import com.eastfair.projectcore.vo.HotImagePointVO;
import com.eastfair.projectcore.vo.HotImageVO;
import com.eastfair.boot.service.SuperCacheServiceImpl;
import com.eastfair.cache.projectcore.HotImageCacheKeyBuilder;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.model.CacheKeyBuilder;
import com.eastfair.database.mybatis.conditions.Wraps;
import com.eastfair.database.mybatis.conditions.query.QueryWrap;

import com.eastfair.uid.model.SelfUidDto;
import com.eastfair.uid.service.SelfUidService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 业务实现类
 * 电子橱窗表
 * </p>
 *
 * @author ligang
 * @date 2021-08-09
 */
@Slf4j
@Service
public class HotImageServiceImpl extends SuperCacheServiceImpl<HotImageMapper, HotImage> implements HotImageService {
    @Autowired
    SelfUidService selfUidService;

    @Autowired
    HotImagePointService hotImagePointService;

    /**
     * 处理新增相关处理
     *
     * @param model 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerSave(HotImage model) {
        //雪花ID,不需要分库分表
        SelfUidDto selfUidDto = new SelfUidDto();
        model.setId(selfUidService.getUid(selfUidDto));
        if (model.getSequence() == null) {
            //排序
            model.setSequence(1);
            if (model.getExhibitionId() != null) {
                model.setSequence(querySort(model.getExhibitionId()));
            }
        }
        return R.successDef();
    }

    /**
     * 排序
     *
     * @param exhibitionId 展会id
     * @return
     */
    public int querySort(Long exhibitionId) {
        QueryWrapper<HotImage> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(HotImage::getExhibitionId, exhibitionId);
        queryWrapper.lambda().eq(HotImage::getDeleted, BusinessConstant.DELETE_NO);
        //排序查询
        queryWrapper.orderByAsc("sequence");
        //条件查询电子橱窗
        List<HotImage> hotImageList = list(queryWrapper);
        if (hotImageList != null && !hotImageList.isEmpty()) {
            int max = hotImageList.stream().mapToInt(s -> s.getSequence()).max().getAsInt();
            return max + 1;
        }
        return 1;
    }


    @Override
    protected CacheKeyBuilder cacheKeyBuilder() {
        return new HotImageCacheKeyBuilder();
    }

    @Override
    public List<HotImageVO> findIdAll(Long exhibitionId) {
        HotImage hotImageWhere = new HotImage();
        hotImageWhere.setExhibitionId(exhibitionId);
        hotImageWhere.setDeleted(BusinessConstant.DELETE_NO);
        hotImageWhere.setRecommend(true);
        QueryWrap<HotImage> wrapper = Wraps.q(hotImageWhere);
        //排序查询
        wrapper.orderByAsc("sequence");
        //条件查询电子橱窗
        List<HotImage> hotImageList = list(wrapper);
        List<HotImageVO> hotImageVOList = doToVos(hotImageList);
        if (hotImageVOList != null && !hotImageVOList.isEmpty()) {
            for (HotImageVO hotImageVo : hotImageVOList) {
                List<HotImagePointVO> hotImagePointVoList = hotImagePointService.findIdAll(hotImageVo.getId());
                hotImageVo.setHotImagePointVoList(hotImagePointVoList);
            }
        }
        return hotImageVOList;
    }

    @Override
    public R recommendHotImage(Long id, Boolean isRecommend) {
        if (isRecommend == null) {
            isRecommend = true;
        }
        HotImage hotImage = getById(id);
        if (hotImage != null) {
            hotImage.setRecommend(isRecommend);
            updateById(hotImage);
        }
        return R.success();
    }

    @Override
    public R top(Long id) {
        HotImage hotImage = getById(id);
        if (hotImage != null) {
            hotImage.setSequence(1);
            updateById(hotImage);
            //查询剩下的展会橱窗信息
            HotImage hotImageWhere = new HotImage();
            hotImageWhere.setExhibitionId(hotImage.getExhibitionId());
            hotImageWhere.setDeleted(BusinessConstant.DELETE_NO);
            QueryWrap<HotImage> wrapper = Wraps.q(hotImageWhere);
            List<HotImage> hotImageList = list(wrapper);
            int num = 2;
            // TODO: 2021/8/12 批量更新
            if (hotImageList != null && !hotImageList.isEmpty()) {
                for (HotImage hotImage1 : hotImageList) {
                    if (hotImage1.getId().equals(id)) {
                        //重新设置排序
                        hotImage1.setSequence(num++);
//                        updateById(hotImage1);
                    }
                }
                //批量更新
                saveOrUpdateBatch(hotImageList);
            }
        }
        return R.success();
    }

    public HotImageVO doToVo(HotImage hotImage) {
        HotImageVO hotImageVo = new HotImageVO();
        BeanUtils.copyProperties(hotImage, hotImageVo);
        hotImageVo.setId(hotImage.getId());
        return hotImageVo;
    }

    public List<HotImageVO> doToVos(List<HotImage> hotImageList) {
        if (hotImageList == null || hotImageList.isEmpty()) {
            return null;
        }
        List<HotImageVO> result = new ArrayList<>();
        for (HotImage hotImage : hotImageList) {
            result.add(doToVo(hotImage));
        }
        return result;
    }


}
